package cn.lixu._02xhr;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

/**
 * @author LiXu
 * @date 2022/08/17 0017 23:51:39
 */
public class Producter_1 {
    public static void main(String[] args) throws Exception {
        //1.创建连接，获取对象
        DefaultMQProducer producer = new DefaultMQProducer("xhr_producter");
        //2.设置连接地址值
        producer.setNamesrvAddr("localhost:9876");
        //3.启动
        producer.start();
        //4.创建消息，并进行设置,发送
        Message message = new Message("hello_chengdu", "chengdu", "我曾踏足山巅，也曾跌落低谷".getBytes(StandardCharsets.UTF_8));
        producer.send(message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println(sendResult.toString()+"这是成功的发送了");
            }

            @Override
            public void onException(Throwable throwable) {
                System.out.println(throwable.toString()+"这是发送失败了");
            }
        });
        System.out.println("这是一次异步请求");
    }
}
